<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>首页</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    {
      //  打乱数组顺序
      let arr = ["😊", 67, true, "55"]
      arr = arr.sort(() => 0.5 - Math.random())
      console.table("打乱数组顺序", arr)
    }; {
      // 去除数字之外的所有字符
      let str = "xiaocheng 123, suoyi haode 120_1qh2"
      let str1 = str.replace(/\D/ig, "")
      console.log(str, str1)
    }; {
      //  反转字符串或者单词
      let str = "xiaocheng 123, suoyi haode 120_1qh2"
      let reverseBySeparator = (str, separator) => {
        return str.split(separator).reverse().join(separator)
      }
      let str1 = reverseBySeparator(str, "")
      let str2 = reverseBySeparator(str, " ")
      console.log(str)
      console.log("按字符", str1)
      console.log("按空格", str2)
    }; {
      //  合并多个对象
      let city = {
        name: "beijing",
        popu: "2w",
      }
      let location = {
        longitude: "106.55",
        latitude: "29.56",
      }
      let obj = { ...city, ...location }
      let obj1 = Object.assign(city, location)
      console.log("合并多个对象", obj)
      console.log("合并多个对象", obj1)
    }; {
      console.log("判断回文字符串")
      // 判断回文字符串: 正着写和反着写都一样的字符串
      let isRevervse = (str1, str2) => {
        let normalize = str => str.toLowerCase()
          .split("")
          .reverse("")
          .join("")
        return normalize(str1) === str2
        console.log(normalize(str1))
      }
      console.log(isRevervse("ramc", "cmar"))
    }; {
      // 从数组中随机选择一个值
      let arr = ["😊", 67, true, "55"]
      let random = arr => arr[Math.floor(Math.random() * arr.length)]
      console.log("从数组中随机选择一个值", random(arr))
    }; {
      // 从 RGB 转换为 HEX
      let rgbToHex = (...arg) => {
        let str = `#`
        arg.forEach(v => {
          let hex = v.toString(16)
          str += hex.length === 1 ? `0${hex}` : hex
        })
        return str
      }
    }; {
      // 过滤数组中值为 false 的值
      let nums = [1, 0, undefined, null, false]
      let newNums = nums.filter(Boolean)
      console.log("过滤数组中值为 false 的值", newNums)
    }; {
      // 空值合并运算符
      // ?? 是一个逻辑操作符，当左侧的操作数为 null 或者 undefined 时，返回其右侧操作数，否则返回左侧操作数
      let null1 = null
      let str = ''
      let num1 = 12

      let i = null1 ?? "IS null"
      let j = str ?? "Defalut"
      let k = num1 ?? "100"
      console.log(i, j, k)
    }; {
      // 字符串转为数组的 4 中方法
      let txt = "abc";

      // split() 方法
      let arr1 = txt.split("");
      console.log(arr1);

      // 展开运算符
      let arr2 = [...txt];
      console.log(arr2);

      // 解构赋值
      let [...arr3] = txt;
      console.log(arr3);

      // Array.from
      let arr4 = Array.from(txt);
      console.log(arr4);
    }
  };
</script>

</html>